package com.zyy.study.always.code4;

import lombok.*;

import java.math.BigDecimal;
import java.time.LocalDate;

@AllArgsConstructor
@NoArgsConstructor
@Data
@ToString
public class BankAccount {
    private String id;
    private String accountName;
    private BigDecimal money;
    private BigDecimal depositMoney;
    private BigDecimal withdrawMoney;
    private LocalDate date;
    private int singleLimit = 10000;
    private int dayLimit=50000;
    private boolean isActive;

    public BankAccount(String id, BigDecimal money, String accountName) {
        this.id = id;
        this.money = money;
        this.accountName = accountName;
        this.depositMoney = new BigDecimal(0);
        this.withdrawMoney = new BigDecimal(0);
        this.date = LocalDate.now();
        isActive = true;
    }

    //查询账户余额
    public synchronized String getBalances() {
        if (!isActive) {
            System.out.println("账户冻结中");
            return null;
        }
        return this.money.toString();
    }

    //存款
    public synchronized void deposit(double m) {
        if (!isActive) {
            System.out.println("账户冻结中");
            return;
        }
        BigDecimal money = new BigDecimal(m);
        if (!validateDeposit(money)) {
            System.out.println("存款失败");
            return;
        }
        this.money=this.money.add(money);
        updateDeposit(money);
    }
    //取款
    public synchronized void withdraw(double m) {
        if (!isActive) {
            System.out.println("账户冻结中");
            return;
        }
        BigDecimal money = new BigDecimal(m);
        if (!validateWithdraw(money)) {
            System.out.println("取款失败");
            return;
        }
        this.money=this.money.subtract(money);
        updateWithdraw(money);
    }

    /**
     * 判断存款是否超额
     * @param money
     * @return
     */
    public boolean validateDeposit(BigDecimal money) {
        //取款金额不能小于0,单次限额不能大于10000
        if (money.compareTo(new BigDecimal("0")) <= 0||money.compareTo(new BigDecimal(singleLimit))>0) {
            return false;
        }
        //更新时间
        if (!LocalDate.now().equals(date)) {
            date = LocalDate.now();
            withdrawMoney = new BigDecimal("0");
        }
        //单日取款额度不能超过50000
        if (depositMoney.add(money).compareTo(new BigDecimal(dayLimit)) > 0) {
            return false;
        }
        return true;
    }

    /**
     * 判断取款是否超额
     * @param money
     * @return
     */
    public boolean validateWithdraw(BigDecimal money) {
        //取款金额不能小于0,单次限额不能大于10000
        if (money.compareTo(new BigDecimal("0")) <= 0||money.compareTo(new BigDecimal(singleLimit))>0) {
            return false;
        }
        //更新时间
        if (!LocalDate.now().equals(date)) {
            date = LocalDate.now();
            withdrawMoney = new BigDecimal("0");
        }
        //单日存款额度不能超过50000
        if (withdrawMoney.add(money).compareTo(new BigDecimal(dayLimit)) > 0) {
            return false;
        }
        return true;
    }
    //更新每日存款总额
    public void updateDeposit(BigDecimal money) {
        depositMoney=depositMoney.add(money);
    }
    //更新每日取款总额
    public void updateWithdraw(BigDecimal money) {
        withdrawMoney=withdrawMoney.add(money);
    }


}
